public with sharing class ext_ReplyToPost {

    Public Instagram_Conversation__c insta {get;set;}
    public Id instaId {get;set;}
    public String userData {get;set;}


    public ext_ReplyToPost(ApexPages.StandardController controller) {
        if(ApexPages.CurrentPage().getParameters().get('id') != null){
            instaId = ApexPages.CurrentPage().getParameters().get('id');
            
            insta = [SELECT Parent__c, Parent__r.Media_ID__c, Reply_To_Post__c, Author_Username__c From Instagram_Conversation__c where id=:instaId];
            insta.Reply_To_Post__c = '@' + insta.Author_Username__c;
        }
    }
    
    public PageReference replyToPost(){
	  	 	Instagram_Token__c token = [SELECT Access_Token__c, User_Id__c FROM Instagram_Token__c LIMIT 1]; 
	        //hardcode values for POC Instagram button leads
	        String access_token = token.Access_Token__c;
    	
            string url = 'https://api.instagram.com/v1/media/' + insta.Parent__r.Media_id__c + '/comments';
            HttpRequest req = new HttpRequest();
            req.setEndpoint(url);
            req.setMethod('POST');
            req.setBody('access_token='+access_token+'&text='+insta.Reply_To_Post__c);
            Http h = new Http();
            HttpResponse res = h.send(req);
            system.debug(res);
            userData = res.getBody();      	
    	
       		return null;
    }

}